package com.jeff.tianti.controller;

import com.alibaba.fastjson.JSONObject;
import com.jeff.tianti.cms.dto.font.*;
import com.jeff.tianti.cms.entity.*;
import com.jeff.tianti.cms.service.*;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/center")//个人中心模块
public class PersonalCenterController {
    /**
     * @Table:consult_compliance
     * @Ceator:liuYI
     * @Describe:个人中心_我的咨询_法律合规咨询数据列表
     */
    @RequestMapping(value = "/compliance/list", method = RequestMethod.POST)
    public AjaxResult findListByUid(@RequestBody ConsultComplianceTo consultComplianceTo) {
        PageModel<ConsultCompliance> page = consultComplianceService.queryConsultCompliancePages(consultComplianceTo);
//        //状态:0->咨询中 1->已完成
        page.getList().stream().forEach(t -> {
            t.setState("1".equals(t.getState()) ? "已完成" : "咨询中");
        });
        return AjaxResult.setSuccessPageModelDate(page);
    }

    @Autowired
    private ConsultComplianceService consultComplianceService;

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的咨询_法律合规咨询_新建咨询
     */
    @RequestMapping(value = "/compliance/save", method = RequestMethod.POST)
    public AjaxResult saveCompliance(@RequestBody ConsultCompliance consultCompliance) {
        if (StringUtils.isNotBlank(consultCompliance.getId())) {//更新
            consultComplianceService.update(consultCompliance);
        } else {//新建
            consultCompliance.setState(ConsultCompliance.STATE_ONE);
            consultCompliance.setNumber(DateUtils.getnum());
            consultCompliance.setAuditFlag(BaseEntity.AUDIT_FLAG_NOT);
            consultComplianceService.save(consultCompliance);
        }
        return AjaxResult.setSuccessData(null);
    }

    @Autowired
    private CourseCounselService courseCounselService;

    /**
     * @Table:course_counsel
     * @Ceator:liuYI
     * @Describe:个人中心_我的咨询_培训课程咨询数据列表
     */
    @RequestMapping(value = "/counsel/list", method = RequestMethod.POST)
    public AjaxResult findLis1tByUid(@RequestBody PageBeanUid pageBean) {
        PageModel<CourseCounsel> page = courseCounselService.findByUid(pageBean);
        return AjaxResult.setSuccessPageModelDate(page);
    }

    @Autowired
    private ConsultCategoryService consultCategoryService;
    @Autowired
    private ConsultService consultService;

    /**
     * @Table:consult_category
     * @Ceator:liuYI
     * @Describe:个人中心_我的咨询_知识产权数据列表
     */
    @RequestMapping(value = "/category/list", method = RequestMethod.POST)
    public AjaxResult findLi2tByUid(@RequestBody PageBeanUid pageBean) {
        PageModel<Consult> page = consultService.findByUid(pageBean);
        return AjaxResult.setSuccessPageModelDate(page);
    }

    @Autowired
    private MediaService mediaService;

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_媒体供需列表
     */
    @RequestMapping(value = "/media/list", method = RequestMethod.POST)
    public AjaxResult findMediaByUid(@RequestBody PageBeanUid pageBean) {
        if (StringUtils.isBlank(pageBean.getUid())) {
            throw new RuntimeException("uid:参数缺失");
        }
        Pageable pageable = new PageRequest(pageBean.getCurrentPage(), pageBean.getPageSize());
        Page<Media> page = mediaService.findByUid(pageBean.getUid(), pageable);
        return AjaxResult.setSuccessPageDate(page);
    }

    @Autowired
    private ResumeService resumeService;

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的求职列表
     */
    @RequestMapping(value = "/resume/list", method = RequestMethod.POST)
    public AjaxResult findResume(@RequestBody PageBeanUid pageBean) {
        int index = (pageBean.getCurrentPage() - 1) * pageBean.getPageSize();//开始索引
        List<Job> list = jobService.findByUidAndPage(pageBean.getUid());
        return AjaxResult.setSuccessData(list);
    }/* *//**
     * @Ceator:liuYI
     * @Describe:test
     */
    /*
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public AjaxResult test(@RequestBody PageBeanUid pageBean) {
        Pageable pageable = new PageRequest(pageBean.getCurrentPage(), pageBean.getPageSize());
//        Page<Resume> page = resumeService.findByUidAndPage(pageBean.getUid(), pageable);
        List<Job> byUidAndPage = jobService.findByUidAndPage(pageBean.getUid());
        return AjaxResult.setSuccessData(byUidAndPage);
    }*/

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的简历
     */
    @RequestMapping(value = "/myResume/list", method = RequestMethod.POST)
    public AjaxResult findMyResume(@RequestBody PageBeanUid pageBean) {
        Pageable pageable = new PageRequest(pageBean.getCurrentPage(), pageBean.getPageSize());
        Page<Resume> page = resumeService.findByUidAndPage(pageBean.getUid(), pageable);
        return AjaxResult.setSuccessData(page);
    }

    @Autowired
    private JobService jobService;

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的招聘列表
     */
    @RequestMapping(value = "/job/list", method = RequestMethod.POST)
    public AjaxResult findJob(@RequestBody JobTo jobTo) {
        PageModel<Resume> page = jobService.queryJobPages(jobTo);
        return AjaxResult.setSuccessPageModelDate(page);
    }

    @Autowired
    private AnswersService answersService;
    @Autowired
    private QuestionsAnswersService questionsAnswersService;

    /**
     * @Ceator:liuYI
     * @Param:type: 0->我的提问 1->我的回答 2->审核中问答
     * @Describe:个人中心_我的问答
     */
    @RequestMapping(value = "/answers/list", method = RequestMethod.POST)
    public AjaxResult findAnswersByUid(@RequestBody AnswersTo answersTo) {
        String type = answersTo.getType() == null ? "0" : answersTo.getType();
        Pageable pageable = new PageRequest(answersTo.getCurrentPage(), answersTo.getPageSize());
        Page page;
        if ("2".equals(type)) {//获取审核中问答
            page = questionsAnswersService.findByUidAndTypeIsAuditFlag(answersTo.getUid(), pageable);
        } else {
            if ("0".equals(type)) {//0->我的提问
                page = answersService.findPageByqueUid(answersTo.getUid(), pageable);
            } else {//1->我的回答
                page = answersService.findPageByUid(answersTo.getUid(), pageable);
            }
        }
        return AjaxResult.setSuccessPageDate(page);
    }

    @Autowired
    private JobResumeMiddleService jobResumeMiddleService;

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的通知
     */
    @RequestMapping(value = "/inform/list", method = RequestMethod.POST)
    public AjaxResult findJobResMeddleList(@RequestBody PageBeanUid pageBeanUid) {
        Pageable pageable = new PageRequest(pageBeanUid.getCurrentPage(), pageBeanUid.getPageSize());
        Page<JobResumeMiddle> page = jobResumeMiddleService.findJobResMeddleListById(pageBeanUid.getUid(), pageable);
        return AjaxResult.setSuccessPageDate(page);
    }

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的职位_停止招聘
     */
    @RequestMapping(value = "/job/del", method = RequestMethod.POST)
    public AjaxResult delJob(@RequestBody JobTo jobTo) {
        Job job = jobService.find(jobTo.getId());
        if (!job.getUid().equals(jobTo.getUid())) {
            throw new RuntimeException("非法操作");
        }
        job.setDeleteFlag(BaseEntity.DELETE_FLAG_DELETED);
        jobService.update(job);
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的职位_恢复招聘/
     */
    @RequestMapping(value = "/job/revocation", method = RequestMethod.POST)
    public AjaxResult revocationJob(@RequestBody JobTo jobTo) {
        Job job = jobService.find(jobTo.getId());
        if (!job.getUid().equals(jobTo.getUid())) {
            throw new RuntimeException("非法操作");
        }
        job.setDeleteFlag(BaseEntity.DELETE_FLAG_NORMAL);
        jobService.update(job);
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的职位_撤销发布
     */
    @RequestMapping(value = "/job/revocationIssue", method = RequestMethod.POST)
    public AjaxResult revocationIssue(@RequestBody JobTo jobTo) {
        Job job = jobService.find(jobTo.getId());
        if (!job.getUid().equals(jobTo.getUid())) {
            throw new RuntimeException("非法操作");
        }
        jobService.delete(job.getId());
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的简历_取消申请
     */
    @RequestMapping(value = "/job/cancel", method = RequestMethod.POST)
    public AjaxResult delJobResMiddle(@RequestBody JobTo jobTo) {
        jobResumeMiddleService.deleteByJobIdAndUid(jobTo.getId(), jobTo.getUid());
        return AjaxResult.setSuccessData(null);
    }


    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的简历_用户删除简历
     */
    @RequestMapping(value = "/resume/del", method = RequestMethod.POST)
    public AjaxResult delJobResMiddle(@RequestBody ResumeTo resumeTo) {
        Resume resume = resumeService.find(resumeTo.getId());
        if (!resume.getUid().equals(resumeTo.getUid())) {
            throw new RuntimeException("非法操作");
        }
        resumeService.delete(resumeTo.getId());
        return AjaxResult.setSuccessData(null);
    }


    /**
     * @Ceator:liuYI
     * @Describe:个人中心_我的简历_数据回显
     */
    @RequestMapping(value = "/resume/details", method = RequestMethod.GET)
    public AjaxResult resumeDetails(@RequestParam("id") String id) {
        Resume resume = resumeService.find(id);
        return AjaxResult.setSuccessData(resume);
    }

    @Autowired
    private GateUserService gateUserService;

    /**
     * @Ceator:liuYI
     * @Describe:个人中心修改资料
     */
    @RequestMapping(value = "/personal/data", method = RequestMethod.POST)
    public AjaxResult personalData(@RequestBody UserGateTo userGateTo) {
        GateUser gateUser = gateUserService.find(userGateTo.getUid());
        if (!gateUser.getId().equals(userGateTo.getUid())) {
            throw new RuntimeException("非法操作");
        }
        GateUser of = UserGateTo.of(userGateTo,gateUser);
        gateUserService.update(of);
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:个人中心用户资料数据回显
     */
    @RequestMapping(value = "/personal/details", method = RequestMethod.POST)
    public AjaxResult personalDetails(@RequestBody PageBeanUid uid) {
        GateUser gateUser = gateUserService.find(uid.getUid());
        return AjaxResult.setSuccessData(UserGateTo.of(gateUser));
    }
}
